package org.jeecg.modules.service.impl;

import cn.hutool.core.util.ObjectUtil;
import org.apache.poi.hslf.record.CString;
import org.jeecg.common.constant.CommonCuckoo;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.entity.CuVersionControl;
import org.jeecg.modules.mapper.CuVersionControlMapper;
import org.jeecg.modules.service.ICuVersionControlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: 版本控制
 * @Author: jeecg-boot
 * @Date: 2024-07-12
 * @Version: V1.0
 */
@Service
public class CuVersionControlServiceImpl extends ServiceImpl<CuVersionControlMapper, CuVersionControl> implements ICuVersionControlService {

    private final String REDIS_KEY = "version_control";

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public boolean save(CuVersionControl cuVersionControl) {
        redisUtil.del(REDIS_KEY);
        return super.save(cuVersionControl);
    }

    @Override
    public boolean updateById(CuVersionControl cuVersionControl) {
        redisUtil.del(REDIS_KEY);
        return super.updateById(cuVersionControl);
    }

    @Override
    public boolean removeById(String id) {
        redisUtil.del(REDIS_KEY);
        return super.removeById(id);
    }

    @Override
    public boolean removeByIds(List<String> ids) {
        redisUtil.del(REDIS_KEY);
        return super.removeByIds(ids);
    }

    @Override
    public List<CuVersionControl> getVersionControl() {
        List<CuVersionControl> list = (List<CuVersionControl>) redisUtil.get(REDIS_KEY);
        if (ObjectUtil.isNotEmpty(list)) {
            return list;
        }

        list = super.list();
        if (ObjectUtil.isEmpty(list)) {
            return null;
        }

        redisUtil.set(REDIS_KEY, list, CommonCuckoo.CACHE_TIME);
        return list;
    }
}
